package si.unimb.client;

import java.io.DataInputStream;
import java.io.IOException;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import si.unimb.engine.*;

public class InputThread extends Thread {
	
	private DataInputStream in;
	private Client c;
	public long ping;
	private Platno p;

	private boolean prvic;
	private boolean dead;
	
	public InputThread(Client c, DataInputStream in){
		this.in = in;
		this.c = c;
		setPrvic(true);
		setDead(false);
		ping = 0;
	}
	
	public void run() {
		while(true){
			try{
				byte identifikator = in.readByte();

				//identifikator 1 = game mode
				if(identifikator == 1){
					
					short id = in.readShort();
					short x = in.readShort();
					short y = in.readShort();

					premakniMe(id, x, y);
					c.addText("id: " + id + ", x: " + x + ", y: " + y);
					
				//identifikator 0 = sporocilo
				}else{
					int length = in.readInt();
					byte[] b = new byte[length];
					in.readFully(b, 0, length);
					
					if(isPrvic()){
						c.setId((short)length);
						setPrvic(false);
						c.addText("New id: " + length);
						System.out.println("New id: " + length);
					}else{
						commandListner(new String(b));
						c.addText(Long.toString(System.currentTimeMillis() - ping));
						c.addText(new String(b));
						System.out.println(Long.toString(System.currentTimeMillis() - ping));
						System.out.println(new String(b));
					}
				}

			} catch(SocketException e){
				c.addText("Server disconnected!");
				closeSocket();
				break;
			} catch(IOException e){
				c.addText("Napaka pri prenosu podatkov!");
				closeSocket();
				break;
			}
		}
	}
	
	public void closeSocket(){
		try{
			in.close();
			c.getOutputStream().close();
			c.getSocket().close();
		} catch(IOException e){
			e.printStackTrace();
		}
	}
	
	public void commandListner(String s){
		s = s.toLowerCase();
		
		if(s.equals("/stopgame")){}
		
		//Zacetek igre
		if(s.contains("/weareready")){
			List<Player> igralci = new ArrayList<Player>();
			StringTokenizer st = new StringTokenizer(s, ":");
			st.nextToken();
			while(st.hasMoreTokens()){
				Player pl = new Player(c, st.nextToken(), Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
				igralci.add(pl);
			}
			setP(new Platno(igralci, c));
		}
	}
	
	public int i = 0;
	public int stevec = 0;
	
	public void premakniMe(short id, short x, short y){
		System.out.println("Cakam: " + ++stevec);
		i++;
		if(i == p.getAktivni_igralci().size()){
			p.repaint();
			i = 0;
		}
		for(Player pl : getP().getAktivni_igralci())
			if(pl.getId() == id)
				pl.getClen().sprejmiPozicijo(x, y);
	}

	public void setDead(boolean dead) {
		this.dead = dead;
	}

	public boolean isDead() {
		return dead;
	}

	public void setPrvic(boolean prvic) {
		this.prvic = prvic;
	}

	public boolean isPrvic() {
		return prvic;
	}

	public void setP(Platno p) {
		this.p = p;
	}

	public Platno getP() {
		return p;
	}
}
